define(['jquery', 'bootstrap', 'backend', 'table', 'form','editable'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'returnpurchasedetail/index' + location.search,
                    add_url: 'returnpurchasedetail/add'+ location.search,//配合index.html 24行的 data-params使用
                    adds_url: 'returnpurchasedetail/adds',
                    edit_url: 'returnpurchasedetail/edit',
                    del_url: 'returnpurchasedetail/del',
                    multi_url: 'returnpurchasedetail/multi',
                    import_url: 'returnpurchasedetail/import',
                    table: 'returnpurchasedetail',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'goods_name', title: __('商品名称')},
                        {field: 'price', title: __('单价'), editable: true,
                        // {
                        //         type: 'text',
                        //         success:function (data,newValue,row) {
                        //             Layer.confirm(
                        //                 '是否确认修改?',
                        //                 {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                        //                 function (index) {
                        //                     var idss = Table.api.selectedids(table);
                        //                     // console.log(idss);
                        //                     console.log(data);
                        //                     console.log(newValue);
                        //                     console.log(row);
                        //                     Fast.api.ajax({
                        //                         url: 'returnpurchasedetail/ajchange?type=price&idss='+idss,
                        //                         type: "post",
                        //                         data: {'changething': newValue},
                        //                     }, function () {
                        //                     });
                        //                     Layer.close(index);
                        //                     table.bootstrapTable('refresh', {});
                        //                 }
                        //
                        //             );
                        //             return false;//这句话要用到，防止未确认，就提交
                        //         }
                        //     }
                            operate: false},
                        {field: 'num', title: __('数量'), editable: true, operate: false},
                        {field: 'price*num', title: __('总价'),
                            formatter:
                                function(value, row, index){
                                    total_price = parseFloat(row.price) * parseFloat(row.num);
                                    return total_price.toFixed(2);
                            },operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            // buttons:[
                            //     {
                            //         name:'detail',//名称
                            //         text:'详情',
                            //         title:'详情',
                            //         classname: 'btn btn-xs btn-info btn-view',
                            //         icon: 'fa fa-check',//图标 可在添加菜单规则处 搜索图标见示例图
                            //         url: 'returnpurchasedetail/index',//接口地址 控制器名/方法名
                            //     }
                            // ],
                            formatter: Table.api.formatter.operate}
                    ]
                ],
                onEditableSave:function (field, row, oldValue, $el) {
                    var data = {};  //组合更新数据
                    data["row[field]"] =field;  //字段名
                    data["row[" + field + "]"] = row[field];    //字段值
                    data["row[id]"] = row['id'];    //主键
                    Fast.api.ajax({
                        url: $.fn.bootstrapTable.defaults.extend.edit_url + "/ids/" + row[this.pk],
                        data: data
                    },function () {
                        $(".btn-refresh").trigger("click");
                    });
                },
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
